<?php
namespace app\common\audit\logic;


use app\common\audit\model\AuditConfig;
use app\common\BaseLogic;

class AuditConfigLogic extends BaseLogic
{
    public function getAudit($companyId, $type) {
        $cacheName = "api-audit-advantage:{$companyId}:$type";
        $data = $this->redis->hGetAll($cacheName);

        if (empty($data['id'])) {
            $fields = "create_time,update_time,delete_time";
            $where = [];
            $where['company_id'] = $companyId;
            $where['timing'] = $type;
            $model = new AuditConfig();
            $row = $model->withoutField($fields)->where($where)->find();
            if (!empty($row)) {
                $data = $row->getData();
                $this->redis->hMSet($cacheName, $data);
            } else {
                $data = ['id'=>0];
                $this->redis->hMSet($cacheName, $data);
            }
        }
        $data['condition'] = empty($data['condition']) ? [] : json_decode($data['condition'], true);

        return $data;
    }

    public function resetAuditCache($audit) {
        $cacheName = "api-audit-advantage:{$audit['company_id']}:{$audit['timing']}";

        $this->redis->del($cacheName);
        $this->getAudit($audit['company_id'], $audit['timing']);
    }
}